//
//  ZYReportContentView.m
//  ZYOils
//
//  Created by 段新瑞 on 2019/9/24.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYReportContentView.h"

#import "QFDatePickerView.h"

#import "NSDate+BinAdd.h"

#import "HRDatePickerWindow.h"
#import "ZYOils-Swift.h"
#import "ZYStateMentModel.h"

@interface ZYReportContentView()

/// 日报周报月报 view
@property (weak, nonatomic) IBOutlet UIView *dateChooseView;

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *chooseButtons;

/// 2019年12月第四周
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

/// 8.26-9.1
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

/// 收取金额
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;

/// 总人数
@property (weak, nonatomic) IBOutlet UILabel *totalPersonLabel;

/// 总笔数
@property (weak, nonatomic) IBOutlet UILabel *reportCountLabel;

/// 单笔均价
@property (weak, nonatomic) IBOutlet UILabel *averageMoneyLabel;

/// 优惠金额
@property (weak, nonatomic) IBOutlet UILabel *welfareMoneyLabel;

@property (weak, nonatomic) IBOutlet UILabel *desLabel;


@property (nonatomic, strong) UIButton *selBtn;

@property (nonatomic, strong) ZYTriangleView *arrowView;

@property (nonatomic, strong) HRDatePickerWindow *datePicker;

@property (nonatomic, strong) ZYStateMentModel *stateMentModel;

@property (nonatomic) NSInteger myTag;

@property (nonatomic, copy) NSString *year;
@property (nonatomic, copy) NSString *month;
@property (nonatomic, copy) NSString *day;

@end


@implementation ZYReportContentView


- (instancetype)initWithCoder:(NSCoder *)coder {
    if (self = [super initWithCoder:coder]) {
        self.dateChooseView.layer.cornerRadius = 7;
        self.clipsToBounds = YES;
        self.myTag = 100;
        NSDate *date;
        if (@available(iOS 13.0, *)) {
            date = [NSDate now];
        } else {
            // Fallback on earlier versions
            date = [NSDate date];
        }
        self.year = STR_LONG(date.year);
        self.month = STR_LONG(date.month);
        self.day = STR_LONG(date.day);
        [self configData];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.dateChooseView.layer.cornerRadius = 7;
    self.clipsToBounds = YES;
    self.selBtn = self.chooseButtons.firstObject;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [self.dateLabel addGestureRecognizer:tap];
    NSDate *date;
    if (@available(iOS 13.0, *)) {
        date = [NSDate now];
    } else {
        // Fallback on earlier versions
        date = [NSDate date];
    }
    
    self.dateLabel.text = [NSString stringWithFormat:@"%lu年%lu月%lu日", (unsigned long)date.year, date.month, date.day];
    
    @weakify(self)
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable sender) {
        NSLog(@"选择时间");
        @strongify(self)
        
        self.myTag = self.selBtn.tag;
        if (self.selBtn.tag == 100) {
            
            NSString *birthdayStr =  self.companyDateModel != nil ? [NSString stringWithFormat:@"%@-%@-%@", self.companyDateModel.year, self.companyDateModel.month, self.companyDateModel.day] : @"2000-03-28";
            NSDate *minDate = [NSDate dateWithString:birthdayStr format:@"yyyy-MM-dd"];
            self.datePicker.minimumDate = minDate;
            [self.datePicker show];
        } else if (self.selBtn.tag == 200) {
            
            NSInteger year = self.companyDateModel.year != nil ?  self.companyDateModel.year.integerValue : 2000;
            NSInteger month = self.companyDateModel.year != nil ?  self.companyDateModel.month.integerValue : 3;
            QFDatePickerView *datePickerView = [[QFDatePickerView alloc]initDatePackerWithStartYear:year  startMonth:month superView:self.superview response:^(NSString *str) {
                @strongify(self)
                self.dateLabel.text = str;
                [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
            }];
            
            [datePickerView.subject subscribeNext:^(NSDictionary *dict) {
                @strongify(self)
                self.year = dict[@"year"];
                self.month = dict[@"year"];
                [self configData];
            }];
            
            [datePickerView show];
        } else if (self.selBtn.tag == 300) {
            
        }
    }];
    
    ZYTriangleView *view = [ZYTriangleView new];
    self.arrowView = view;
    [self addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.dateLabel.mas_right);
        make.centerY.mas_equalTo(self.dateLabel);
        make.width.height.mas_equalTo(5);
    }];
}

- (IBAction)dayMonthYearBtnClick:(UIButton *)sender {
    if ([self.selBtn isEqual:sender]) {
        return;
    }
    self.selBtn = sender;
    [self.chooseButtons enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger index, BOOL * _Nonnull stop) {
        btn.selected = NO;
    }];
    self.arrowView.hidden = NO;
    NSDate *date;
    if (@available(iOS 13.0, *)) {
        date = [NSDate now];
    } else {
        // Fallback on earlier versions
        date = [NSDate date];
    }
    
    self.myTag = self.selBtn.tag;
    
    /// 日报
    if (self.selBtn.tag == 100) {
        self.dateLabel.text = [NSString stringWithFormat:@"%lu年%lu月%lu日", (unsigned long)date.year, date.month, date.day];
        self.year = STR_LONG(date.year);
        self.month = STR_LONG(date.month);
        self.day = STR_LONG(date.day);
        [self configData];
    }
    
    /// 月报
    else if (self.selBtn.tag == 200) {
        self.dateLabel.text = [NSString stringWithFormat:@"%lu年%lu月", (unsigned long)date.year, date.month];
        self.year = STR_LONG(date.year);
        self.month = STR_LONG(date.month);
        [self configData];
    }
    
    /// 总报
    else if (self.selBtn.tag == 300) {
        //TODO: 总报不知道接口该如何请求,所以未做请求
        self.dateLabel.text = @"总报";
        self.arrowView.hidden = YES;
        [self configData];
    }
    
    self.selBtn.selected = YES;
}

- (void)configData {
    
    /// 获取账单数据
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:ZYUserInfoModel.sharedInfoModel.userModel.managerId forKey:@"managerId"];
    if (self.myTag == 100) {
        [dict setValue:self.year forKey:@"year"];
        [dict setValue:self.month forKey:@"month"];
        [dict setValue:self.day forKey:@"day"];
    }
    else if (self.myTag == 200) {
        [dict setValue:self.year forKey:@"year"];
        [dict setValue:self.month forKey:@"month"];
    } else {
        
    }
    
    [ZYHud showLoadingText:@""];
    [[DataManager manager] postStatementParam:dict block:^(XRCommonModel *model, NSError *error) {
        [ZYHud hideHud];
        if (model.isSuccess) {
            self.stateMentModel = [ZYStateMentModel mj_objectWithKeyValues:model.data];
            
            self.moneyLabel.text = self.stateMentModel.totalMoney;
            self.totalPersonLabel.text = self.stateMentModel.userCount;
            self.reportCountLabel.text = self.stateMentModel.totalBill;
            self.averageMoneyLabel.text = self.stateMentModel.averPrice;
            self.desLabel.text = self.stateMentModel.message;
            
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

//懒加载
- (HRDatePickerWindow *)datePicker{
    if (!_datePicker) {
        _datePicker = [[HRDatePickerWindow alloc] init];
        _datePicker.datePickerMode = HRDatePickerModeDate;
        _datePicker.operationBackGroundColor = COLORHEX(@"8e929f");
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文显示
        _datePicker.datePicker.locale = locale;
        _datePicker.showTodayButton = YES;
        _datePicker.maskColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        _datePicker.maximumDate = [NSDate date];
        __weak typeof(self) weakSelf = self;
        _datePicker.commitBlock = ^(NSDate *date) {
            //选定日期的回调, 处理如改变UI, 发送请求等业务逻辑
            weakSelf.dateLabel.text = [NSString stringWithFormat:@"%ld年%ld月%ld日", date.year, date.month, date.day];
            weakSelf.year = STR_LONG(date.year);
            weakSelf.month = STR_LONG(date.month);
            weakSelf.day = STR_LONG(date.day);
            [weakSelf configData];
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        };
    }
    return _datePicker;
}


- (ZYStateMentModel *)stateMentModel {
    if (!_stateMentModel) {
        _stateMentModel = [[ZYStateMentModel alloc] init];
    }
    return _stateMentModel;
}

@end
